﻿Public Class imageComboBox
    Inherits System.Windows.Forms.ComboBox

    Private _imageList As ImageList

    Public Property ImageList() As ImageList
        Get
            Return _imageList
        End Get
        Set(ByVal value As ImageList)
            _imageList = value
        End Set
    End Property

    Public Sub New()
        DrawMode = DrawMode.OwnerDrawFixed
    End Sub

    Protected Overloads Overrides Sub OnDrawItem(ByVal ea As DrawItemEventArgs)
        ea.DrawBackground()
        ea.DrawFocusRectangle()
        Dim item As imageComboBoxItem
        Dim imageSize As Size = _imageList.ImageSize
        Dim bounds As Rectangle = ea.Bounds
        Try
            item = Me.Items(ea.Index)
            If item.ImageIndex <> -1 Then
                _imageList.Draw(ea.Graphics, bounds.Left, bounds.Top, item.ImageIndex)
                ea.Graphics.DrawString(item.Text, ea.Font, New SolidBrush(ea.ForeColor), bounds.Left + imageSize.Width, bounds.Top)
            Else
                ea.Graphics.DrawString(item.Text, ea.Font, New SolidBrush(ea.ForeColor), bounds.Left, bounds.Top)
            End If
        Catch
            If ea.Index <> -1 Then
                ea.Graphics.DrawString(Items(ea.Index).ToString, ea.Font, New SolidBrush(ea.ForeColor), bounds.Left, bounds.Top)
            Else
                ea.Graphics.DrawString(Text, ea.Font, New SolidBrush(ea.ForeColor), bounds.Left, bounds.Top)
            End If
        End Try
        MyBase.OnDrawItem(ea)
    End Sub

End Class